package com.macken.wow.util;

import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.DigestUtils;

public class SignatureUtil {

	public static final String seed = "U2FsdGVkX19tPz8IzQbB3+C1NLrrhkXwl3NZej9Q+nYs3opmk9a16g==";

	static final Logger logger = LoggerFactory.getLogger(SignatureUtil.class);

	public static String md5(Object... args) {
		StringBuilder sb = new StringBuilder();
		for (Object arg : args)
			if (arg != null)
				sb.append(arg);
		return DigestUtils.md5DigestAsHex(sb.toString().getBytes());
	}
	public static String sha(Object... args) {
		StringBuilder sb = new StringBuilder();
		for (Object arg : args)
			if (arg != null)
				sb.append(arg);

		return "";
	}

	public static String sha1(String input) throws NoSuchAlgorithmException {
		MessageDigest mDigest = MessageDigest.getInstance("SHA");
		
		byte[] result = mDigest.digest(input.getBytes());
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < result.length; i++) {
			sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
		}
		return sb.toString();
	}
	public static String encGbk(String param) {
		return enc(param, "gb18030");
	}

	public static String encUtf8(String param) {
		return enc(param, "utf-8");
	}

	private static String enc(String param, String encoding) {
		try {
			return URLEncoder.encode(param, encoding);
		} catch (Exception e) {
			logger.error("should not error. (encode):" + param, e);
			return param;
		}
	}
	public static String getUserToken(long uid) {
		String key = uid + "|minisogoukey";
		return org.apache.commons.codec.digest.DigestUtils.md5Hex(key);
	}
	public static void main(String[] args) throws Exception{
		System.out.println(sha1("TAN:TAN").toUpperCase());
	}

}
